#!/bin/bash

# 设置UTF-8编码
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8

# 获取脚本所在目录
cd "$(dirname "$0")"
PROJECT_DIR=$(pwd)

# 终端样式
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
BOLD='\033[1m'

clear
echo ""
echo "================================================"
echo "    建党在线系统 - 构建并启动全部服务"
echo "================================================"
echo ""

# 询问是否继续
read -p "是否继续？(Y/N): " choice
if [[ ! "$choice" =~ ^[Yy]$ ]]; then
    echo "${BLUE}[INFO]${NC} 操作已取消"
    echo ""
    read -n 1 -s -r -p "按任意键退出..."
    exit 0
fi

echo ""
echo "${BLUE}[INFO]${NC} 正在检查环境..."
echo ""

# 检查Java环境
echo "${BLUE}[INFO]${NC} 检查Java环境..."
if ! command -v java &> /dev/null; then
    echo "${RED}[ERROR]${NC} 未检测到Java环境，请先安装JDK 1.8+"
    echo ""
    read -n 1 -s -r -p "按任意键退出..."
    exit 1
fi
java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
echo "${GREEN}✓${NC} Java版本: $java_version"

# 检查Maven环境
echo "${BLUE}[INFO]${NC} 检查Maven环境..."
if ! command -v mvn &> /dev/null; then
    echo "${RED}[ERROR]${NC} 未检测到Maven环境，请先安装Maven"
    echo ""
    read -n 1 -s -r -p "按任意键退出..."
    exit 1
fi
mvn_version=$(mvn -version 2>&1 | head -n 1)
echo "${GREEN}✓${NC} $mvn_version"

# 检查Node.js环境
echo "${BLUE}[INFO]${NC} 检查Node.js环境..."
if ! command -v node &> /dev/null; then
    echo "${RED}[ERROR]${NC} 未检测到Node.js环境，请先安装Node.js"
    echo ""
    read -n 1 -s -r -p "按任意键退出..."
    exit 1
fi
node_version=$(node -v)
echo "${GREEN}✓${NC} Node.js版本: $node_version"

echo ""
echo "${GREEN}[INFO]${NC} 所有环境检查通过！"
echo ""

# 检查并构建前端
echo "================================================"
echo "检查前端构建文件"
echo "================================================"

# 检查管理端
if [ ! -d "$PROJECT_DIR/src/main/resources/client/dist" ]; then
    echo "${YELLOW}[WARN]${NC} 管理端未构建，开始构建..."
    cd "$PROJECT_DIR/src/main/resources/client"
    
    # 检查node_modules
    if [ ! -d "node_modules" ]; then
        echo "${BLUE}[INFO]${NC} 安装管理端依赖..."
        npm install --registry=https://registry.npmmirror.com --legacy-peer-deps
    fi
    
    echo "${BLUE}[INFO]${NC} 构建管理端..."
    npm run build
    
    if [ $? -eq 0 ]; then
        echo "${GREEN}✓${NC} 管理端构建成功"
    else
        echo "${RED}[ERROR]${NC} 管理端构建失败"
        echo ""
        read -n 1 -s -r -p "按任意键退出..."
        exit 1
    fi
else
    echo "${GREEN}✓${NC} 管理端已构建"
fi

# 检查用户端
if [ ! -d "$PROJECT_DIR/src/main/resources/front/dist" ]; then
    echo "${YELLOW}[WARN]${NC} 用户端未构建，开始构建..."
    cd "$PROJECT_DIR/src/main/resources/front"
    
    # 检查node_modules
    if [ ! -d "node_modules" ]; then
        echo "${BLUE}[INFO]${NC} 安装用户端依赖..."
        npm install --registry=https://registry.npmmirror.com --legacy-peer-deps
    fi
    
    echo "${BLUE}[INFO]${NC} 构建用户端..."
    npm run build
    
    if [ $? -eq 0 ]; then
        echo "${GREEN}✓${NC} 用户端构建成功"
    else
        echo "${RED}[ERROR]${NC} 用户端构建失败"
        echo ""
        read -n 1 -s -r -p "按任意键退出..."
        exit 1
    fi
else
    echo "${GREEN}✓${NC} 用户端已构建"
fi

cd "$PROJECT_DIR"

echo ""
echo "================================================"
echo "[1/3] 启动后端服务 (Spring Boot)"
echo "================================================"
echo "${BLUE}[INFO]${NC} 正在启动后端服务，请稍候..."

# 在新终端窗口中启动后端服务
osascript <<EOF
tell application "Terminal"
    do script "cd '$PROJECT_DIR' && echo '================================================' && echo '  建党在线系统 - 后端服务' && echo '================================================' && echo '' && echo '[INFO] 正在启动Spring Boot后端服务...' && echo '' && mvn spring-boot:run; exec bash"
    set custom title of front window to "建党在线系统 - 后端服务"
end tell
EOF

echo "${GREEN}✓${NC} 后端服务启动命令已发送"
echo "${BLUE}[INFO]${NC} 等待后端服务启动..."
sleep 15

echo ""
echo "================================================"
echo "[2/3] 启动管理端前端"
echo "================================================"
echo "${BLUE}[INFO]${NC} 正在启动管理端前端..."

osascript <<EOF
tell application "Terminal"
    do script "cd '$PROJECT_DIR/src/main/resources/client' && echo '================================================' && echo '  建党在线系统 - 管理端前端' && echo '================================================' && echo '' && echo '[INFO] 正在启动管理端前端...' && echo '' && node server.js; exec bash"
    set custom title of front window to "建党在线系统 - 管理端前端"
end tell
EOF

echo "${GREEN}✓${NC} 管理端前端启动命令已发送"
echo "${BLUE}[INFO]${NC} 等待管理端启动..."
sleep 3

echo ""
echo "================================================"
echo "[3/3] 启动用户端前端"
echo "================================================"
echo "${BLUE}[INFO]${NC} 正在启动用户端前端..."

osascript <<EOF
tell application "Terminal"
    do script "cd '$PROJECT_DIR/src/main/resources/front' && echo '================================================' && echo '  建党在线系统 - 用户端前端' && echo '================================================' && echo '' && echo '[INFO] 正在启动用户端前端...' && echo '' && node server.js; exec bash"
    set custom title of front window to "建党在线系统 - 用户端前端"
end tell
EOF

echo "${GREEN}✓${NC} 用户端前端启动命令已发送"

# 完成提示
echo ""
echo "================================================"
echo "           🎉 启动完成！"
echo "================================================"
echo ""
echo "${BLUE}[INFO]${NC} 所有服务正在启动中，请稍候片刻..."
echo ""
echo "📍 服务访问地址："
echo "   ├─ 后端API:    ${GREEN}http://localhost:8080/springboot1i659${NC}"
echo "   ├─ 管理端:     ${GREEN}http://localhost:8088${NC}"
echo "   └─ 用户端:     ${GREEN}http://localhost:8089${NC}"
echo ""
echo "👤 默认账号信息："
echo "   ${BOLD}管理端${NC} (http://localhost:8088)"
echo "   ├─ 用户名: ${GREEN}java1234${NC}"
echo "   └─ 密码:   ${GREEN}123456${NC}"
echo ""
echo "   ${BOLD}用户端${NC} (http://localhost:8089)"
echo "   ├─ 党员账号:   ${GREEN}党员账号1${NC} / 密码: ${GREEN}123456${NC}"
echo "   └─ 支部账号:   ${GREEN}支部账号1${NC} / 密码: ${GREEN}123456${NC}"
echo ""
echo "💡 温馨提示："
echo "   • ${YELLOW}首次构建需要下载依赖，可能需要10-20分钟${NC}"
echo "   • 后续启动会快很多，因为已经构建好了"
echo "   • 如遇到端口冲突，请检查端口是否被占用"
echo "   • 要停止服务，请关闭对应的终端窗口"
echo ""
echo "${BLUE}[INFO]${NC} 启动器已完成，各服务将在新终端窗口中运行"
echo ""
read -n 1 -s -r -p "按任意键退出启动器..."
echo ""

